package com.azkj.album.network.networkframe.bean;

import android.graphics.Bitmap;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.BufferedSink;

public class BitmapRequestBody extends RequestBody {
    private Bitmap bitmap;

    public BitmapRequestBody(Bitmap bitmap) {
        this.bitmap = bitmap;

    }
    @Override
    public MediaType contentType() {
        return MediaType.parse("image/png");
    }

    @Override
    public void writeTo(BufferedSink sink) throws IOException {
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, sink.outputStream());
    }


    @Override
    public boolean isOneShot() {
        // 此处返回true，防止添加了日志拦截器导致，
        // 上传文件时writeTo函数2次调用（因为日志拦截器里面也调用了一次）
        return true;
    }

}
